﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;
using System.Web.Routing;

namespace Wicresoft.SMS.Web
{
    public static class Pagination
    {

        private static string Key = "";
        private static string Url = "";
        private static string Orderby = "";
        private static int MaxPage;
        private static int Index;

        private static DateTime Starttime;
        private static DateTime Endtime;

        private static string PrePage = "";
        private static string NextPage = "";

        public static void ShowPagination(this HtmlHelper html, int index, int maxpage, string keyword)
        {
            if (maxpage == 0)
                return;
            Index = index;
            MaxPage = maxpage;

            //if (keyword != null)
            //    MaxPage = Convert.ToInt32(keyword);

            Url = HttpContext.Current.Request.Url.AbsolutePath;
            if (keyword != null)
                Key = keyword;
            else
                Key = "";
            HtmlTextWriter writer = new HtmlTextWriter(html.ViewContext.Writer);
            WriteHtml(writer);

            PrePage = "";
            NextPage = "";
            Orderby = "";
            Starttime = DateTime.MinValue;
            Endtime = DateTime.MinValue;
        }

        public static void ShowPagination(this HtmlHelper html, int index, int maxpage, string keyword, bool IsChangeUrl)
        {
            if (maxpage == 0)
                return;
            Index = index;
            MaxPage = maxpage;

            //if (keyword != null)
            //    MaxPage = Convert.ToInt32(keyword);
            if(IsChangeUrl)
            Url = HttpContext.Current.Request.Url.AbsolutePath;
  
            if (keyword != null)
                Key = keyword;
            else
                Key = "";
            HtmlTextWriter writer = new HtmlTextWriter(html.ViewContext.Writer);
            WriteHtml(writer);

            PrePage = "";
            NextPage = "";
            Orderby = "";
            Starttime = DateTime.MinValue;
            Endtime = DateTime.MinValue;
        }

        public static void ShowPagination(this HtmlHelper html, int index, int maxpage, string keyword, string prepage, string nextpage)
        {
            PrePage = prepage;
            NextPage = nextpage;
            ShowPagination(html, index, maxpage, keyword);
        }

        public static void ShowPagination(this HtmlHelper html, int index, int maxpage, string keyword, string prepage, string nextpage, string orderby)
        {
            Orderby = orderby;
            ShowPagination(html, index, maxpage, keyword, prepage, nextpage);
        }

        public static void ShowPagination(this HtmlHelper html, int index, int maxpage, string keyword, string prepage, string nextpage, string orderby, DateTime starttime, DateTime endtime)
        {
            Starttime = starttime;
            Endtime = endtime;
            ShowPagination(html, index, maxpage, keyword, prepage, nextpage,orderby);
        }

        public static void ShowPagination(this HtmlHelper html, int index, int maxpage, string keyword, bool IsChangeUrl, string prepage, string nextpage)
        {
            PrePage = prepage;
            NextPage = nextpage;
            ShowPagination(html, index, maxpage, keyword, IsChangeUrl);
        }

        private static void WriteHtml(HtmlTextWriter writer)
        {
            writer.RenderBeginTag(HtmlTextWriterTag.Ul); // <ul>
            BuildPrevOrNextPage(writer, PrevOrNext.Prev);
            BulidNumbers(writer);
            BuildPrevOrNextPage(writer, PrevOrNext.Next);
            writer.RenderEndTag(); // </ul>
        }

        private static void BulidNumbers(HtmlTextWriter writer)
        {
            if (MaxPage <= 8)
            {
                for (int i = 1; i <= MaxPage; i++)
                {
                    if (i == Index)
                    {
                        BulidNumberLink(writer, i.ToString(), "current");
                    }
                    else
                        BulidNumberLink(writer, i.ToString(), null);
                }
            }
            else
            {
                if (Index == 1)
                {
                    BulidNumberLink(writer, "1", "current");
                    BulidNumberLink(writer, (Index + 1).ToString(), null);
                    BulidNumberLink(writer, (Index + 2).ToString(), null);
                    PrintDot(writer, 2);
                }
                else if (Index == 2)
                {
                    BulidNumberLink(writer, (Index - 1).ToString(), null);
                    BulidNumberLink(writer, Index.ToString(), "current");
                    BulidNumberLink(writer, (Index + 1).ToString(), null);
                    PrintDot(writer, 2);
                }
                else if (Index == 3)
                {
                    BulidNumberLink(writer, "1", null);
                    BulidNumberLink(writer, (Index - 1).ToString(), null);
                    BulidNumberLink(writer, Index.ToString(), "current");
                    BulidNumberLink(writer, (Index + 1).ToString(), null);
                    PrintDot(writer, 2);
                }
                else if (Index == 4)
                {
                    BulidNumberLink(writer, "1", null);
                    PrintDot(writer, 1);
                    BulidNumberLink(writer, (Index - 1).ToString(), null);
                    BulidNumberLink(writer, Index.ToString(), "current");
                    BulidNumberLink(writer, (Index + 1).ToString(), null);
                    PrintDot(writer, 2);
                }
                else if (MaxPage - Index > 3)
                {
                    BulidNumberLink(writer, "1", null);

                    PrintDot(writer, 2);
                    BulidNumberLink(writer, (Index - 1).ToString(), null);
                    BulidNumberLink(writer, Index.ToString(), "current");
                    BulidNumberLink(writer, (Index + 1).ToString(), null);
                    PrintDot(writer, 2);
                }
                else if (MaxPage - Index == 3)
                {
                    BulidNumberLink(writer, "1", null);

                    PrintDot(writer, 2);
                    BulidNumberLink(writer, (Index - 1).ToString(), null);
                    BulidNumberLink(writer, Index.ToString(), "current");
                    BulidNumberLink(writer, (Index + 1).ToString(), null);
                    PrintDot(writer, 1);
                }
                else if (MaxPage - Index == 2)
                {
                    BulidNumberLink(writer, "1", null);
                    PrintDot(writer, 2);
                    BulidNumberLink(writer, (Index - 1).ToString(), null);
                    BulidNumberLink(writer, Index.ToString(), "current");
                    BulidNumberLink(writer, (Index + 1).ToString(), null);
                }
                else if (MaxPage - Index == 1)
                {
                    BulidNumberLink(writer, "1", null);
                    PrintDot(writer, 2);
                    BulidNumberLink(writer, (Index - 1).ToString(), null);
                    BulidNumberLink(writer, Index.ToString(), "current");
                }


                if (Index == MaxPage)
                {
                    BulidNumberLink(writer, "1", null);
                    PrintDot(writer, 2);
                    BulidNumberLink(writer, (Index - 2).ToString(), null);
                    BulidNumberLink(writer, (Index - 1).ToString(), null);
                    BulidNumberLink(writer, MaxPage.ToString(), "current");
                }
                else
                    BulidNumberLink(writer, MaxPage.ToString(), null);
            }
        }

        private static void BulidNumberLink(HtmlTextWriter writer, string page, string currentcss)
        {
            string url = Url;
            if (Key != "")
            {
                url += "?key=" + Key + "&page=" + page;
            }
            else
            {
                url += "?page=" + page;
            }
            if (Starttime != DateTime.MinValue && Endtime != DateTime.MinValue)
            {
                url += "&starttime=" + Starttime.ToString("yyyy/MM/dd") + "&endtime=" + Endtime.ToString("yyyy/MM/dd");
            }
            if (Orderby != "")
            {
                url += "&orderby=" + Orderby;
            }
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "pagelinks");
            writer.RenderBeginTag(HtmlTextWriterTag.Li); //<li>
            writer.AddAttribute(HtmlTextWriterAttribute.Title, page);
            writer.AddAttribute(HtmlTextWriterAttribute.Href, url);
            if (currentcss != null)
                writer.AddAttribute(HtmlTextWriterAttribute.Class, currentcss);
            writer.RenderBeginTag(HtmlTextWriterTag.A);  //<a>
            writer.Write(page);
            writer.RenderEndTag(); //</a>
            writer.RenderEndTag(); // </li>
        }

        private static void PrintDot(HtmlTextWriter writer, int count)
        {
            for (int i = 0; i < count; i++)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Class, "pagelinks");
                writer.RenderBeginTag(HtmlTextWriterTag.Li); //<li>
                writer.Write(".");
                writer.RenderEndTag(); // </li>
            }
        }

        private static void BuildPrevOrNextPage(HtmlTextWriter writer, PrevOrNext pon)
        {
            string url = Url;

            string css = "";
            string pic = "";
            string str = "";
            string page = "";
            switch (pon)
            {
                case PrevOrNext.Prev:
                    css = "pagepreviou";
                    if (PrePage == "")
                        pic = "../../Content/images/page_previou_icon.gif";
                    else
                        pic = PrePage;
                    str = "上一页";
                    if (Index == 1)
                        page = "1";
                    else
                        page = (Index - 1).ToString();
                    break;
                case PrevOrNext.Next:
                    css = "pagenext";
                    if (NextPage == "")
                        pic = "../../Content/images/page_next_icon.gif";
                    else
                        pic = NextPage;
                    str = "下一页";
                    if (Index == MaxPage)
                        page = MaxPage.ToString();
                    else
                        page = (Index + 1).ToString();
                    break;
                default:
                    return;
            }
            if (Key != "")
            {
                url += "?key=" + Key + "&page=" + page;

            }
            else
            {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
                url += "?page=" + page;
            }
            if (Starttime != DateTime.MinValue && Endtime != DateTime.MinValue)
            {
                url += "&starttime=" + Starttime.ToString("yyyy/MM/dd") + "&endtime=" + Endtime.ToString("yyyy/MM/dd");
            }
            if (Orderby != "")
            {
                url += "&orderby=" + Orderby;
            }
            writer.AddAttribute(HtmlTextWriterAttribute.Class, css);
            writer.RenderBeginTag(HtmlTextWriterTag.Li); //<li>
            writer.AddAttribute(HtmlTextWriterAttribute.Title, str);
            writer.AddAttribute(HtmlTextWriterAttribute.Href, url);
            writer.RenderBeginTag(HtmlTextWriterTag.A);  //<a>
            writer.AddAttribute(HtmlTextWriterAttribute.Border, "0");
            writer.AddAttribute(HtmlTextWriterAttribute.Alt, str);
            writer.AddAttribute(HtmlTextWriterAttribute.Src, pic);
            writer.RenderBeginTag(HtmlTextWriterTag.Img); //<img>
            writer.RenderEndTag(); //</img>
            writer.RenderEndTag(); //</a>
            writer.RenderEndTag(); // </li>
        }

        enum PrevOrNext
        {
            Prev,
            Next
        }
    }
}